
package com.kweative.lingua.server;

import com.kweative.lingua.server.annotations.MethodInfo;
import com.kweative.lingua.server.structures.DataStore;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 *
 * @author henrik
 */
public class Controller<A extends Application> implements Serializable {
    private A app = null;
    private DataStore<String,Object> datastore = null;
    
    @MethodInfo(nonpublic=true)
    public String getName() {
        return getClass().getSimpleName();
    }
    @MethodInfo(nonpublic=true)
    public void setApp(A app) {
        this.app = app;
        if (app != null) {
            datastore = new DataStore<String, Object>(getApp().getName()+"_"+getName() + "_datastore");
        }
    }
    @MethodInfo(nonpublic=true)
    public A getApp() {
        return this.app;
    }
    @MethodInfo(nonpublic=true)
    public Method[] getMethods() {
        Method methods[] = this.getClass().getMethods();
        ArrayList<Method> result = new ArrayList<Method>();
        for(Method method:methods) {
            if (method.getName().equals("wait")
                || method.getName().equals("getClass")
                || method.getName().equals("notify")
                || method.getName().equals("notifyAll")
                || method.getName().equals("hashCode")
                || method.getName().equals("equals")
                || method.getName().equals("toString"))
                continue;
            MethodInfo annotation = method.getAnnotation(MethodInfo.class);
            if (annotation != null && annotation.nonpublic())
                continue;
            result.add(method);
        }
        return result.toArray(new Method[0]);
    }
}
